﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using System.Data;

public partial class Clientes : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void cmbCity_OnCallback(object source, CallbackEventArgsBase e)
    {
        FillCityCombo(source as ASPxComboBox, e.Parameter);
    }

    private DataView GetCities(string country)
    {
        List<string> list = new List<string>();
        Municipios.SelectParameters[0].DefaultValue = country;
        DataView view = (DataView)Municipios.Select(DataSourceSelectArguments.Empty);
        //for (int i = 0; i < view.Count; i++)
        //{
        //    list.Add((string)view[i][0]);
        //}
        return view;
    }
    protected void FillCityCombo(ASPxComboBox cmb, string country)
    {
        if (string.IsNullOrEmpty(country)) return;

        //List<string> cities = GetCities(country);
        cmb.Items.Clear();

        DataView view = GetCities(country);
        cmb.DataSource = view;
        cmb.TextField = "ZON_NOMBRE";
        cmb.ValueField = "ZON_ID";
        cmb.DataBind();
        //foreach (string city in cities)
        //    cmb.Items.Add(city);
    }


   
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        ASPxGridViewExporterClientes.WriteXlsToResponse();
    }
    protected void ASPxGridViewClientes_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        //if (!ASPxGridView2.IsEditing || e.Column.FieldName != "MUNICIPIO") return;
        if (e.Column.FieldName != "MUNICIPIO") 
            return;
        
            ASPxComboBox combo = e.Editor as ASPxComboBox;
            combo.Callback += new CallbackEventHandlerBase(cmbCity_OnCallback);
        
        if (e.KeyValue == DBNull.Value || e.KeyValue == null) return;
        object val = ASPxGridViewClientes.GetRowValuesByKeyValue(e.KeyValue, "ZON_PADRE_ID");
        if (val == DBNull.Value) return;
        string country = (string)val;

        
        FillCityCombo(combo, country);

        
    }
    protected void ASPxGridViewClientes_CustomButtonInitialize(object sender, ASPxGridViewCustomButtonEventArgs e)
    {

    }
    protected void ASPxGridViewClientes_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
    {

    }
    protected void ASPxGridViewClientes_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {

    }
}